#include "common.h"

int dfs(int n, TreeNode* root)
{
	if (root == nullptr) return 0;
	int m = n * 10 + root->val;
	if (root->left == nullptr && root->right == nullptr) return m;
	if (root->left == nullptr) return dfs(m, root->right);
	if (root->right == nullptr) return dfs(m, root->left);
	return dfs(m, root->left) + dfs(m, root->right);
}
int sumNumbers(TreeNode* root)
{
	return dfs(0, root);
}